/**
 * @author BoLiang
 */
if (typeof accedo === "undefined") {
	accedo = {};
}
if (typeof accedo.achievement === "undefined") {
	accedo.achievement = {};
}

accedo.achievement.achiContShow = {

	oldOnkeydownFunc : null,
	divContCtnNode : null,

	openPage : function(achi_sn) {
		this.divContCtnNode = null;
		AchiService._path = "http://"
								+ accedo.achievement.config.serverIP + ":" 
								+ accedo.achievement.config.serverPort + "/" 
								+ accedo.achievement.config.pluginName + "/dwr";
		AchiService.getJsonAchiByAchisn(achi_sn, this.openPageCallback);
	},
	openPageCallback : function(jsonData) {
		// alert(data.achi_desc);
		// var jsonData = eval("(" + data + ")");
		// create achievement content show container
		// accedo.achievement.achiContShow.divContCtnNode = document.createElement("div");
		// accedo.achievement.achiContShow.divContCtnNode.id = "divAchiShowContent";
		// accedo.achievement.achiContShow.divContCtnNode.innerHTML = "<font size=13px color=white>" + jsonData.achi_desc + "</font>";
		



		var bodyNode = document.getElementsByTagName("body")[0];
		bodyNode.appendChild(accedo.achievement.achiContShow.createContentNode(jsonData));

		accedo.achievement.achiContShow.oldOnkeydownFunc = document.onkeydown;
		document.onkeydown = function(e) {
			accedo.achievement.achiContShow.keysControl(e);
		};
	},

	createContentNode : function(jsonData) {
		// create achievement content container node
		this.divContCtnNode = document.createElement("div");
		this.divContCtnNode.id = "divAchiShowContent";

		// create title node which includes the star node and name node
		var divTitle = document.createElement("div");
		divTitle.id = "divAchiShowContentTitle";

		var divStar = document.createElement("div");
		divStar.id = "divAchiShowContentStar";
		divStar.innerHTML = "<img width=90% height=90% src='http://"
								  + accedo.achievement.config.serverIP + ":" 
								  + accedo.achievement.config.serverPort + "/" 
								  + accedo.achievement.config.pluginName 
								  + "/achiPlugin/achi-images/achiStar.png' />";

		var divName = document.createElement("div");
		divName.id = "divAchiShowContentName";
		divName.innerHTML = "<font color=white>" + jsonData.achi_name + "</font>";
		divTitle.appendChild(divStar);
		divTitle.appendChild(divName);

		// create score node
		var divScore = document.createElement("div");
		divScore.id = "divAchiShowContentScore";
		divScore.innerHTML = "<font color=white>SCORE: " + jsonData.achi_score + " Points</font>";

		// create detail node
		var divDetail = document.createElement("div");
		divDetail.id = "divAchiShowContentDetail";
		divDetail.innerHTML = "<font color=white>" + jsonData.achi_desc + "</font>";

		// append all of the nodes into div content container node
		this.divContCtnNode.appendChild(divTitle);
		this.divContCtnNode.appendChild(divScore);
		this.divContCtnNode.appendChild(divDetail);

		return this.divContCtnNode; 

	},
	
	closePage : function() {
		var divAchiShowMainNode = document.getElementById("divAchiShowMain");
		var badgeShowMainNode = document.getElementById("badgeShowMainNode");
		var campShowMainNode = document.getElementById("campShowMainNode"); 
		if(divAchiShowMainNode) {
			divAchiShowMainNode.style.opacity = null;
		} else if (badgeShowMainNode) {
			badgeShowMainNode.style.opacity = null;
		} else if (campShowMainNode) {
			campShowMainNode.style.opacity = null;
		}

		var bodyNode = document.getElementsByTagName("body")[0];
		bodyNode.removeChild(this.divContCtnNode);
		document.onkeydown = this.oldOnkeydownFunc;
	},

	keysControl : function(e) {
		e = window.event || e;
		switch (e.keyCode) {
		// esc button
		case 27:
			this.closePage();
			break;
		default:
			break;
		}
	}

};